import React from "react";
import { Form, Select, Input, Space, Button } from "antd";
import { apiUserBanned, apiFreeBanned, apiUnFreeze, apiUnBanned } from '@/request/users'

const { TextArea } = Input;

class Banned extends React.Component {
  finishClick = async value => {
    value = { ...value, memberId: this.props.memberId }
    if (this.props.complaintId) {
      value.memberComplaintId = this.props.complaintId
    }
    if (this.props.type === 'account') {
      if (value.deadline === 9) {
        let unFreeze = await apiUnFreeze(value)
        if (unFreeze.code === 0) {
          React.$message({
            type: 'info',
            content: '解冻成功'
          })
          this.props.reload()
          this.props.dialogClose()
        }
      } else {
        value = {
          ...value,
          freezeReason: value.bannedReason
        }
        delete (value.bannedReason)
        let freezeRes = await apiFreeBanned(value)
        if (freezeRes.code === 0) {
          React.$message({
            type: 'info',
            content: '冻结成功'
          })
          this.props.reload()
          this.props.dialogClose()
        }
      }
    }
    if (this.props.type === 'release') {
      if (value.deadline === 9) {
        let unRes = await apiUnBanned(value)
        if (unRes.code === 0) {
          React.$message({
            type: 'info',
            content: '解封成功'
          })
          this.props.reload()
          this.props.dialogClose()
        }
      } else {
        let res = await apiUserBanned(value)
        if (res.code === 0) {
          React.$message({
            type: 'info',
            content: '封禁成功'
          })
          this.props.reload()
          this.props.dialogClose()
        }
      }
    }
  }
  render() {
    const options = [
      {
        label: this.props.type === 'account' ? "解冻账号" : "解除封禁",
        value: 9,
      },
      {
        label: "1小时",
        value: 1,
      },
      {
        label: "3小时",
        value: 3,
      },
      {
        label: "24小时",
        value: 24,
      },
      {
        label: "48小时",
        value: 48,
      },
      {
        label: "7*24小时",
        value: 168,
      },
      {
        label: "永久",
        value: 0,
      },
    ];
    return (
      <div>
        <Form onFinish={this.finishClick} labelCol={{ span: 5 }} labelAlign="right">
          <Form.Item label="封禁时间" rules={[{ required: true }]} name={'deadline'}>
            <Select options={options} />
          </Form.Item>
          <Form.Item label="备注" name="bannedReason" rules={[{ required: true }]}>
            <TextArea
              allowClear
              autoSize={{ minRows: 6, maxRows: 6 }}
              showCount
              maxLength={50}
            />
          </Form.Item>
          <Form.Item>
            <div
              style={{
                display: "flex",
                justifyContent: "flex-end",
                marginBottom: "-20px",
                marginTop: 20,
              }}
            >
              <Space>
                <Button onClick={this.props.dialogClose}>取消</Button>
                <Button type="primary" htmlType="submit">保存</Button>
              </Space>
            </div>
          </Form.Item>
        </Form>
      </div>
    );
  }
}

export default Banned;
